brushless28 2.2.0
Loading...
Searching...
No Matches
brushless28.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2026 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef BRUSHLESS28_H
29#define BRUSHLESS28_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53#include "drv_pwm.h"
54
60
65
70
75#define BRUSHLESS28_REG_IC_STAT1 0x00
76#define BRUSHLESS28_REG_IC_STAT2 0x01
77#define BRUSHLESS28_REG_IC_STAT3 0x02
78#define BRUSHLESS28_REG_IC_STAT4 0x03
79#define BRUSHLESS28_REG_IC_STAT5 0x04
80#define BRUSHLESS28_REG_IC_STAT6 0x05
81#define BRUSHLESS28_REG_IC_CTRL1 0x1A
82#define BRUSHLESS28_REG_IC_CTRL2 0x1B
83#define BRUSHLESS28_REG_IC_CTRL3 0x1C
84#define BRUSHLESS28_REG_GD_CTRL1 0x1E
85#define BRUSHLESS28_REG_GD_CTRL2 0x1F
86#define BRUSHLESS28_REG_GD_CTRL3 0x21
87#define BRUSHLESS28_REG_GD_CTRL3B 0x22
88#define BRUSHLESS28_REG_GD_CTRL4 0x23
89#define BRUSHLESS28_REG_GD_CTRL5 0x24
90#define BRUSHLESS28_REG_GD_CTRL6 0x25
91#define BRUSHLESS28_REG_GD_CTRL7 0x26
92#define BRUSHLESS28_REG_CSA_CTRL 0x29
93#define BRUSHLESS28_REG_MON_CTRL1 0x2B
94#define BRUSHLESS28_REG_MON_CTRL2 0x2C
95#define BRUSHLESS28_REG_MON_CTRL3 0x2D
96#define BRUSHLESS28_REG_MON_CTRL4 0x2E
97#define BRUSHLESS28_REG_SPI_TEST 0x36
98#define BRUSHLESS28_REG_OTP_USR 0x48
99 // brushless28_reg
101
106
111
116#define BRUSHLESS28_IC_CTRL2_ENABLE_DRV 0x8000
117#define BRUSHLESS28_IC_CTRL2_MODE_NSLEEP 0x4000
118#define BRUSHLESS28_IC_CTRL2_CFG_CRC_EN 0x2000
119#define BRUSHLESS28_IC_CTRL2_CLKMON_EN 0x1000
120#define BRUSHLESS28_IC_CTRL2_CSA_EN 0x0800
121#define BRUSHLESS28_IC_CTRL2_CSA_AZ_DIS 0x0400
122#define BRUSHLESS28_IC_CTRL2_GVDD_MODE_LDO 0x0100
123#define BRUSHLESS28_IC_CTRL2_VCP_MODE_NORMAL 0x0000
124#define BRUSHLESS28_IC_CTRL2_VCP_MODE_SW_DIS 0x0040
125#define BRUSHLESS28_IC_CTRL2_VCP_MODE_SHUTDOWN 0x0080
126#define BRUSHLESS28_IC_CTRL2_VCP_MODE_TCP_DIS 0x00C0
127#define BRUSHLESS28_IC_CTRL2_VCP_MODE_MASK 0x00C0
128#define BRUSHLESS28_IC_CTRL2_LOCK_UNLOCK 0x0006
129#define BRUSHLESS28_IC_CTRL2_LOCK_LOCK 0x000C
130#define BRUSHLESS28_IC_CTRL2_LOCK_MASK 0x000E
131#define BRUSHLESS28_IC_CTRL2_CLR_FLT 0x0001
132
137#define BRUSHLESS28_GD_CTRL1_PWM_MODE_6X_PWM 0x0000
138#define BRUSHLESS28_GD_CTRL1_PWM_MODE_3X_PWM_INLX 0x1000
139#define BRUSHLESS28_GD_CTRL1_PWM_MODE_3X_PWM_SPI 0x2000
140#define BRUSHLESS28_GD_CTRL1_PWM_MODE_1X_PWM 0x3000
141#define BRUSHLESS28_GD_CTRL1_PWM_MODE_SPI_GATE 0x5000
142#define BRUSHLESS28_GD_CTRL1_PWM_MODE_MASK 0x7000
143#define BRUSHLESS28_GD_CTRL1_SGD_MODE_FIXED 0x0000
144#define BRUSHLESS28_GD_CTRL1_SGD_MODE_DYNAMIC 0x0200
145#define BRUSHLESS28_GD_CTRL1_SGD_MODE_MASK 0x0600
146#define BRUSHLESS28_GD_CTRL1_SGD_TMP_EN 0x0100
147#define BRUSHLESS28_GD_CTRL1_STP_MODE_0 0x0000
148#define BRUSHLESS28_GD_CTRL1_STP_MODE_1 0x0080
149#define BRUSHLESS28_GD_CTRL1_STP_MODE_MASK 0x0080
150#define BRUSHLESS28_GD_CTRL1_DEADT_70NS 0x0000
151#define BRUSHLESS28_GD_CTRL1_DEADT_200NS 0x0008
152#define BRUSHLESS28_GD_CTRL1_DEADT_300NS 0x0010
153#define BRUSHLESS28_GD_CTRL1_DEADT_500NS 0x0018
154#define BRUSHLESS28_GD_CTRL1_DEADT_750NS 0x0020
155#define BRUSHLESS28_GD_CTRL1_DEADT_1000NS 0x0028
156#define BRUSHLESS28_GD_CTRL1_DEADT_1500NS 0x0030
157#define BRUSHLESS28_GD_CTRL1_DEADT_2000NS 0x0038
158#define BRUSHLESS28_GD_CTRL1_DEADT_MASK 0x0038
159#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_0 0x0000
160#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_1 0x0004
161#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_MASK 0x0004
162#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_6X_0 0x0000
163#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_6X_1 0x0001
164#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_6X_2 0x0002
165#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_6X_3 0x0003
166#define BRUSHLESS28_GD_CTRL1_DEADT_MODE_6X_MASK 0x0003
167
172#define BRUSHLESS28_SPI_TEST_WORD 0x5AA5
173
178#define BRUSHLESS28_SPI_FRAME_READ_BIT 0x01
179#define BRUSHLESS28_SPI_FRAME_CRC_DISABLE 0x00
180#define BRUSHLESS28_SPI_FRAME_CRC_ENABLE 0x01
181#define BRUSHLESS28_SPI_FRAME_CRC_MASK 0x01
182
187#define BRUSHLESS28_DIR_CW 0
188#define BRUSHLESS28_DIR_CCW 1
189
194#define BRUSHLESS28_DEF_DUTY 0.0
195#define BRUSHLESS28_DEF_FREQ 5000
196
205#define BRUSHLESS28_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
206#define BRUSHLESS28_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
207 // brushless28_set
209
214
219
224#define BRUSHLESS28_MAP_MIKROBUS( cfg, mikrobus ) \
225 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
226 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
227 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
228 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
229 cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
230 cfg.brake = MIKROBUS( mikrobus, MIKROBUS_RST ); \
231 cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
232 cfg.fault = MIKROBUS( mikrobus, MIKROBUS_INT )
233 // brushless28_map // brushless28
236
241typedef struct
242{
243 // Output pins
244 digital_out_t dir;
245 digital_out_t brake;
246
247 // Input pins
248 digital_in_t fault;
249
250 // Modules
251 spi_master_t spi;
252 pwm_t pwm;
253
254 pin_name_t chip_select;
255 uint32_t pwm_freq;
256
257 uint8_t crc_en;
258
260
265typedef struct
266{
267 // Communication gpio pins
268 pin_name_t miso;
269 pin_name_t mosi;
270 pin_name_t sck;
271 pin_name_t cs;
272 pin_name_t pwm;
273
274 // Additional gpio pins
275 pin_name_t dir;
276 pin_name_t brake;
277 pin_name_t fault;
278
279 // static variable
280 uint32_t spi_speed;
281 spi_master_mode_t spi_mode;
282 spi_master_chip_select_polarity_t cs_polarity;
283 uint32_t dev_pwm_freq;
284
286
297
303
314
329
343
356err_t brushless28_write_reg ( brushless28_t *ctx, uint8_t reg, uint16_t data_in );
357
370err_t brushless28_read_reg ( brushless28_t *ctx, uint8_t reg, uint16_t *data_out );
371
383err_t brushless28_set_duty_cycle ( brushless28_t *ctx, float duty_cycle );
384
396
408
419void brushless28_set_direction ( brushless28_t *ctx, uint8_t dir );
420
430
440
450
460
461#ifdef __cplusplus
462}
463#endif
464#endif // BRUSHLESS28_H
465 // brushless28
467
468// ------------------------------------------------------------------------ END
brushless28_return_value_t
Brushless 28 Click return value data.
Definition brushless28.h:292
@ BRUSHLESS28_ERROR
Definition brushless28.h:294
@ BRUSHLESS28_OK
Definition brushless28.h:293
err_t brushless28_set_duty_cycle(brushless28_t *ctx, float duty_cycle)
Brushless 28 sets PWM duty cycle.
err_t brushless28_write_reg(brushless28_t *ctx, uint8_t reg, uint16_t data_in)
Brushless 28 data writing function.
err_t brushless28_default_cfg(brushless28_t *ctx)
Brushless 28 default configuration function.
void brushless28_cfg_setup(brushless28_cfg_t *cfg)
Brushless 28 configuration object setup function.
err_t brushless28_pwm_start(brushless28_t *ctx)
Brushless 28 start PWM module.
err_t brushless28_read_reg(brushless28_t *ctx, uint8_t reg, uint16_t *data_out)
Brushless 28 data reading function.
uint8_t brushless28_get_fault_pin(brushless28_t *ctx)
Brushless 28 get fault pin function.
void brushless28_set_direction(brushless28_t *ctx, uint8_t dir)
Brushless 28 set direction function.
void brushless28_pull_brake(brushless28_t *ctx)
Brushless 28 pull brake function.
err_t brushless28_init(brushless28_t *ctx, brushless28_cfg_t *cfg)
Brushless 28 initialization function.
err_t brushless28_pwm_stop(brushless28_t *ctx)
Brushless 28 stop PWM module.
void brushless28_switch_direction(brushless28_t *ctx)
Brushless 28 switch direction function.
void brushless28_release_brake(brushless28_t *ctx)
Brushless 28 release brake function.
This file contains SPI specific macros, functions, etc.
Brushless 28 Click configuration object.
Definition brushless28.h:266
spi_master_chip_select_polarity_t cs_polarity
Definition brushless28.h:282
pin_name_t sck
Definition brushless28.h:270
pin_name_t brake
Definition brushless28.h:276
spi_master_mode_t spi_mode
Definition brushless28.h:281
pin_name_t mosi
Definition brushless28.h:269
uint32_t spi_speed
Definition brushless28.h:280
uint32_t dev_pwm_freq
Definition brushless28.h:283
pin_name_t dir
Definition brushless28.h:275
pin_name_t pwm
Definition brushless28.h:272
pin_name_t miso
Definition brushless28.h:268
pin_name_t cs
Definition brushless28.h:271
pin_name_t fault
Definition brushless28.h:277
Brushless 28 Click context object.
Definition brushless28.h:242
uint8_t crc_en
Definition brushless28.h:257
spi_master_t spi
Definition brushless28.h:251
uint32_t pwm_freq
Definition brushless28.h:255
digital_out_t brake
Definition brushless28.h:245
digital_in_t fault
Definition brushless28.h:248
pin_name_t chip_select
Definition brushless28.h:254
pwm_t pwm
Definition brushless28.h:252
digital_out_t dir
Definition brushless28.h:244